package com.xyh.concurrency.example.concueerncyAqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * AQS 中的限流组件就是限定流量的组件
 */
@Slf4j
public class SemaphoreDemo {
    private final static int cuont = 20;

    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(3);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < cuont; i++) {
            int cotdowcount = i;
            executorService.execute(() -> {
                try {
                    //获得许可
                    semaphore.acquire();
                    test(cotdowcount);
                    //释放许可
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            });
        }
        log.info("kaishi");
        executorService.shutdown();
    }

    public static void test(int cuont) throws InterruptedException {
        Thread.sleep(100);
        log.info("{}", cuont);

    }
}
